package yunwo.cn.yzstation.view.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.IshoudongView;
import yunwo.cn.yzstation.base.BasicAdapter;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.AssigmnetBean;
import yunwo.cn.yzstation.bean.ContinueBean;
import yunwo.cn.yzstation.bean.ItemlistBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.presenter.ShoudongPersenter;

/**
 * Created by lao on 2017/9/9.
 */
public class ShoudongActivity extends MyBaseActivity implements IshoudongView,View.OnClickListener{

    private ShoudongPersenter mShoudongPersenter;
    private ListView mListview;
    private LinearLayout mLinerlayout;
    private LinearLayout mAllchoose;
    private ImageView mSelectimg;
    private List<ItemlistBean.DataBean> mDataBeanList;
    private TextView mSelectTv;
    private boolean selectAll = true;
    private List<ItemlistBean.DataBean> mSelectedCollectedCars;
    private Myadapter mMyadapter;
    private TextView mTheselected;
    private TextView mXuanding;
    private LinearLayout mXuandingLL;
    private Intent mIntent;
    private AssigmnetBean mAssigmentBean;
    private TextView mComplete;
    private TextView mCarnumber;
    private TextView mBatch;
    private ContinueBean.DataBean mMContinueBean;
    private List<ContinueBean.DataBean.DepartDetailListBean> mDepartDetailList;
    private LinearLayout mStateLL;
    private TextView mStatetv;
    private RelativeLayout mShouhuo_title;
    boolean Isstandard = true;
    private StandardMyadapter mStandardMyadapter;
    private ProgressDialog mMyDialog;
    String departId;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mMyDialog == null) {
            mMyDialog = ProgressDialog.show(ShoudongActivity.this, null, "请稍等...");
            mMyDialog.setCancelable(false);
            mMyDialog.show();
        }
        initView();
        if (getIntent().getSerializableExtra("AssigmentBean") != null) {
            mAssigmentBean = (AssigmnetBean) getIntent().getSerializableExtra("AssigmentBean");
            mCarnumber.setText(mAssigmentBean.getCarnumber());
            mBatch.setText(mAssigmentBean.getBatch());
        }else {
            mMContinueBean = (ContinueBean.DataBean) getIntent().getSerializableExtra("mContinue");
            mCarnumber.setText(mMContinueBean.getPlateNumber());
            mBatch.setText(mMContinueBean.getBatchNumber());
            conversionBean(mMContinueBean);
        }
        mShoudongPersenter.getoBtainTrack(mAssigmentBean,mMContinueBean);
    }

    private void conversionBean(ContinueBean.DataBean mContinueBean) {
        mDepartDetailList = mContinueBean.getDepartDetailList();
        for (int i = 0; i < mDepartDetailList.size(); i++) {
            ItemlistBean.DataBean dataBean = new ItemlistBean.DataBean();
            dataBean.setBeginOrganName(mDepartDetailList.get(i).getBeginOrganName());
            dataBean.setTrackingNumber(mDepartDetailList.get(i).getTrackingNumber());
            dataBean.setSubTrackingNumber(mDepartDetailList.get(i).getSubTrackingNumber());
            dataBean.setGoodsNumber(mDepartDetailList.get(i).getGoodsNumber());
            dataBean.setEndOrganName(mDepartDetailList.get(i).getEndOrganName());
            dataBean.setBeginOrganId(mDepartDetailList.get(i).getEndOrganId());
            dataBean.setWeight(mDepartDetailList.get(i).getWeight());
            dataBean.setFreight(mDepartDetailList.get(i).getFreight());
            dataBean.setEndOrganId(mDepartDetailList.get(i).getEndOrganId());
            dataBean.setVolume(mDepartDetailList.get(i).getVolume());
            dataBean.setConsigneeAddress(mDepartDetailList.get(i).getConsigneeName());

            mSelectedCollectedCars.add(dataBean);

        }
        mXuanding.setText(mSelectedCollectedCars.size()+"");

    }

    private void initView() {
        mMyadapter = new Myadapter(ShoudongActivity.this);
        mDataBeanList = new ArrayList<>();
        mSelectedCollectedCars = new ArrayList<>();
        mShoudongPersenter = new ShoudongPersenter(this, this);

        mListview = (ListView) findViewById(R.id.listview);
        mAllchoose = (LinearLayout) findViewById(R.id.allchoose);
        mSelectimg = (ImageView) findViewById(R.id.selectimg);
        mSelectTv = (TextView) findViewById(R.id.selectTv);
        mTheselected = (TextView) findViewById(R.id.theselected);
        mXuanding = (TextView) findViewById(R.id.xuanding);
        mXuandingLL = (LinearLayout) findViewById(R.id.xuandingLL);
        mComplete = (TextView) findViewById(R.id.complete);
        mCarnumber = (TextView) findViewById(R.id.carnumber);
        mBatch = (TextView) findViewById(R.id.batch);
        mStateLL = (LinearLayout) findViewById(R.id.state);
        mStatetv = (TextView) findViewById(R.id.statetv);
        mShouhuo_title = (RelativeLayout) findViewById(R.id.shouhuo_title);
        mStandardMyadapter = new StandardMyadapter(ShoudongActivity.this);
        mStateLL.setOnClickListener(this);
        mComplete.setOnClickListener(this);
        mXuandingLL.setOnClickListener(this);
        mAllchoose.setOnClickListener(this);
        mTheselected.setOnClickListener(this);



    }

    @Override
    protected String settiltile() {
        return "手动选择";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_shoudong;
    }

    @Override
    public void getoBtainTrackSueecss(List<ItemlistBean.DataBean> dataBeanList) {

        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }

        this.mDataBeanList = dataBeanList;
        mMyadapter = new Myadapter(ShoudongActivity.this);
        mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
        mListview.setAdapter(mMyadapter);


    }

    @Override
    public void completeSueescc() {

        finish();
    }

    @Override
    public void getoBtainTrackfailure() {
        if (mMyDialog != null && mMyDialog.isShowing()) {
            mMyDialog.dismiss();
        }
    }

    @Override
    public void Addarefailure() {

    }

    @Override
    public void AddareaSuccess(String departId) {
        this.departId = departId;
        Toast.makeText(ShoudongActivity.this, "加入选定区成功", Toast.LENGTH_SHORT).show();
        mXuanding.setText(mSelectedCollectedCars.size()+"");
        for (int i = 0; i < mSelectedCollectedCars.size(); i++) {
            if (mDataBeanList.contains(mSelectedCollectedCars.get(i))) {
                mDataBeanList.remove(mSelectedCollectedCars.get(i));
            }
        }
        if (mDataBeanList.size() == 0) {
            mListview.setVisibility(View.GONE);
        }
        mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
        mStandardMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.state:
                if (Isstandard) {
                    Isstandard = false;
                    mStatetv.setText("标准");
                    mShouhuo_title.setVisibility(View.VISIBLE);
                    mStandardMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                    mListview.setAdapter(mStandardMyadapter);


                }else {
                    Isstandard = true;
                    mStatetv.setText("列表");
                    mShouhuo_title.setVisibility(View.GONE);
                    mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                    mListview.setAdapter(mMyadapter);
                }
                break;
            case R.id.complete:
                if (mXuanding.getText().toString().equals("0")) {
                    Toast.makeText(ShoudongActivity.this, "选定区暂无数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                mShoudongPersenter.complete(mAssigmentBean,mSelectedCollectedCars,mMContinueBean);
                break;
            case R.id.allchoose:
                if (mDataBeanList.size() > 0) {
                    if (selectAll) {
                        mSelectTv.setText("取消全选");
                        mSelectimg.setImageResource(R.drawable.btn_gou);
                        mSelectedCollectedCars.addAll(mDataBeanList);
                        selectAll = false;
                    }else {
                        mSelectTv.setText("全选");
                        mSelectimg.setImageResource(R.drawable.btn_quanxuan);
                        mSelectedCollectedCars.clear();
                        selectAll = true;
                    }
                }
                mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                break;
            case R.id.theselected:

                //加入选定区
                if (mSelectedCollectedCars.size() == 0) {
                    Toast.makeText(ShoudongActivity.this,"请选择运单号", Toast.LENGTH_SHORT).show();
                    return;
                }

                mShoudongPersenter.Addarea(mAssigmentBean,mMContinueBean,mSelectedCollectedCars);








               /* mXuanding.setText(mSelectedCollectedCars.size()+"");
                for (int i = 0; i < mSelectedCollectedCars.size(); i++) {
                    if (mDataBeanList.contains(mSelectedCollectedCars.get(i))) {
                        mDataBeanList.remove(mSelectedCollectedCars.get(i));
                    }
                }
                if (mDataBeanList.size() == 0) {
                    mListview.setVisibility(View.GONE);
                }
                mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                mStandardMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);*/

//                Toast.makeText(ShoudongActivity.this, mSelectedCollectedCars.size() + "", Toast.LENGTH_SHORT).show();
                break;


            case R.id.xuandingLL:
                if (mXuanding.getText().toString().equals("0")) {
                    Toast.makeText(ShoudongActivity.this, "选定区暂无数据", Toast.LENGTH_SHORT).show();
                    return;
                }

                mIntent = new Intent(ShoudongActivity.this, LoadedActivity.class);
                mIntent.putExtra("data", (Serializable) mSelectedCollectedCars);
                mIntent.putExtra("departId", departId);
                startActivityForResult(mIntent, MyConstants.requestxuanding);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case MyConstants.Resultrxuanding:
                List<ItemlistBean.DataBean> data1 = (List<ItemlistBean.DataBean>) data.getSerializableExtra("data");

                for (int i = 0; i < data1.size(); i++) {
                    if (mSelectedCollectedCars.contains(data1.get(i))) {
                        mSelectedCollectedCars.remove(data1.get(i));
                    }
                }
                for (int i = 0; i < data1.size(); i++) {
                    if (!mDataBeanList.contains(data1.get(i))) {
                        mDataBeanList.add(data1.get(i));
                    }
                }
                if (mDataBeanList.size() > 0) {
                    mListview.setVisibility(View.VISIBLE);
                }
                mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);

                mXuanding.setText(mSelectedCollectedCars.size()+"");
                break;
        }
    }

    class Myadapter extends BasicAdapter<ItemlistBean.DataBean>{



        public Myadapter(Context mctx) {
            super(mctx);
        }

        @Override
        protected int setItemLayout() {
            return R.layout.item_shoudong;
        }

        @Override
        protected void bindData(View convertView, ItemlistBean.DataBean data, final int position) {
            RelativeLayout autoRL = get(convertView, R.id.autoRL);
            TextView consignor = get(convertView, R.id.consignor);
            TextView consignorphone = get(convertView, R.id.consignorphone);
            TextView consignee = get(convertView, R.id.consignee);
            TextView consigneephone = get(convertView, R.id.consigneephone);
            TextView beginOrganName = get(convertView, R.id.beginOrganName);
            TextView endOrganName = get(convertView, R.id.endOrganName);
            TextView trackingNumber = get(convertView, R.id.trackingNumber);
            TextView state = get(convertView, R.id.state);
            final ImageView mImageView = get(convertView, R.id.imageView);


            if (data.getConsignorName().equals("") && data.getConsignorPhone().equals("")) {
                consignor.setVisibility(View.GONE);
                consignorphone.setText("暂未填写发货人信息");
            }else {
                consignor.setText(data.getConsignorName());
                consignorphone.setText(data.getConsignorPhone());
            }
            consignee.setText(data.getConsigneeName());
            consigneephone.setText(data.getConsigneePhone());
            beginOrganName.setText(data.getBeginOrganName());
            endOrganName.setText(data.getEndOrganName());
            trackingNumber.setText("子运单号："+data.getTrackingNumber());

            if (mDataBeanList.size() >0) {
                if (mSelectedCollectedCars.contains(mDataBeanList.get(position))) {// 点击全部
                    mImageView.setImageResource(R.drawable.btn_banyuan_selected);

                } else {
                    mImageView.setImageResource(R.drawable.btn_banyuan_nor);
                }

                autoRL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mSelectedCollectedCars.contains(mDataBeanList.get(position))) {
                            mImageView.setImageResource(R.drawable.btn_banyuan_selected);
                            mSelectedCollectedCars.add(mDataBeanList.get(position));
                        }else {
                            mImageView.setImageResource(R.drawable.btn_banyuan_nor);
                            mSelectedCollectedCars.remove(mDataBeanList.get(position));
                        }

                        if (mSelectedCollectedCars.size() == mDataBeanList.size()) {
                            mSelectTv.setText("取消全选");
                            mSelectimg.setImageResource(R.drawable.btn_gou);
                        }else {
                            mSelectTv.setText("全选");
                            mSelectimg.setImageResource(R.drawable.btn_quanxuan);

                        }
                    }
                });
            }



            switch (data.getStatus()) {

                case 4:
                    state.setText("待发车");
                    state.setBackgroundResource(R.drawable.list_icon_end);
                    break;
                case 5:
                    state.setText("运输中");
                    state.setBackgroundResource(R.drawable.list_icon_end);
                    break;
                case 6:
                    state.setText("待配送");
                    state.setBackgroundResource(R.drawable.list_icon_end);
                    break;
                case 7:
                    state.setText("待签收");
                    state.setBackgroundResource(R.drawable.list_icon_end);
                    break;
                case 8:
                    state.setText("已签收");
                    state.setBackgroundResource(R.drawable.icon_have_in_hand);
                    break;
            }
        }
    }

    class StandardMyadapter extends BasicAdapter<ItemlistBean.DataBean>{



        public StandardMyadapter(Context mctx) {
            super(mctx);
        }

        @Override
        protected int setItemLayout() {
            return R.layout.select_waybill_adapter1;
        }

        @Override
        protected void bindData(View convertView, ItemlistBean.DataBean data, final int position) {
        /*    RelativeLayout autoRL = get(convertView, R.id.autoRL);
            TextView consignor = get(convertView, R.id.consignor);
            TextView consignorphone = get(convertView, R.id.consignorphone);
            TextView consignee = get(convertView, R.id.consignee);
            TextView consigneephone = get(convertView, R.id.consigneephone);
            TextView beginOrganName = get(convertView, R.id.beginOrganName);
            TextView endOrganName = get(convertView, R.id.endOrganName);
            TextView trackingNumber = get(convertView, R.id.trackingNumber);
            TextView state = get(convertView, R.id.state);
            final ImageView mImageView = get(convertView, R.id.imageView);*/


            LinearLayout view = get(convertView, R.id.itemLL);
            final ImageView img = get(convertView, R.id.ungou);
            TextView waybillTV = get(convertView, R.id.waybillTV);
            TextView consignee = get(convertView, R.id.consignee);
            TextView destinationTV = get(convertView, R.id.destinationTV);
            TextView stateTV = get(convertView, R.id.stateTV);




         /*   if (data.getConsignorName().equals("") && data.getConsignorPhone().equals("")) {
                consignor.setVisibility(View.GONE);
                consignorphone.setText("暂未填写发货人信息");
            }else {
                consignor.setText(data.getConsignorName());
                consignorphone.setText(data.getConsignorPhone());
            }*/
            consignee.setText(data.getConsigneeName());
//            consigneephone.setText(data.getConsigneePhone());
//            beginOrganName.setText(data.getBeginOrganName());
            destinationTV.setText(data.getEndOrganName());
            waybillTV.setText("运单号："+data.getTrackingNumber());

            if (mDataBeanList.size() >0) {
                if (mSelectedCollectedCars.contains(mDataBeanList.get(position))) {// 点击全部
                    img.setImageResource(R.drawable.btn_gou);

                } else {
                    img.setImageResource(R.drawable.btn_quanxuan);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mSelectedCollectedCars.contains(mDataBeanList.get(position))) {
                            img.setImageResource(R.drawable.btn_gou);
                            mSelectedCollectedCars.add(mDataBeanList.get(position));
                        }else {
                            img.setImageResource(R.drawable.btn_quanxuan);
                            mSelectedCollectedCars.remove(mDataBeanList.get(position));
                        }

                        if (mSelectedCollectedCars.size() == mDataBeanList.size()) {
                            mSelectTv.setText("取消全选");
                            mSelectimg.setImageResource(R.drawable.btn_gou);
                        }else {
                            mSelectTv.setText("全选");
                            mSelectimg.setImageResource(R.drawable.btn_quanxuan);

                        }
                    }
                });
            }



            switch (data.getStatus()) {

                case 4:
                    stateTV.setText("待发车");

                    break;
                case 5:
                    stateTV.setText("运输中");

                    break;
                case 6:
                    stateTV.setText("待配送");

                    break;
                case 7:
                    stateTV.setText("待签收");

                    break;
                case 8:
                    stateTV.setText("已签收");

                    break;
            }
        }
    }

}
